package com.woshua.radiogroupmodel.radio;

import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.Color;
import android.util.AttributeSet;
import android.view.View;
import android.view.ViewGroup;
import android.widget.FrameLayout;

import com.woshua.radiogroupmodel.R;

/*
* 单选按钮必备的条件:
* 1.是一个view
* 2.有两种状态选中和没有选中
* 3.暴露当前切换状态
* 4.抽取公共的属性
* 5.方便扩展
* */
public abstract class Selector extends FrameLayout implements View.OnClickListener {

    //单选按钮唯一标示符
    private String tag;

    private SelectorGroup selectorGroup;

    private OnSelectorStateListener stateListener;

    public Selector(Context context) {
        super(context);
        initView(context, null);
    }

    public Selector(Context context, AttributeSet attrs) {
        super(context, attrs);
        initView(context, attrs);
    }

    public Selector(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        initView(context, attrs);
    }

    private void initView(Context context,AttributeSet attrs)
    {
        View view = onCreateView();
        LayoutParams params = new LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT,
                ViewGroup.LayoutParams.MATCH_PARENT);
        this.addView(view, params);
        this.setOnClickListener(this);

        //读取自定义属性
        if (attrs != null) {
            TypedArray typedArray = context.obtainStyledAttributes(attrs, R.styleable.Selector);
            String text = typedArray.getString(R.styleable.Selector_text);
            int iconResId = typedArray.getResourceId(R.styleable.Selector_img, 0);
            int selectorResId = typedArray.getResourceId(R.styleable.Selector_indicator, 0);
            int textColor = typedArray.getColor(R.styleable.Selector_text_color, Color.parseColor("#FF222222"));
            int textSize = typedArray.getInteger(R.styleable.Selector_text_size, 15);
            tag = typedArray.getString(R.styleable.Selector_tag);
            //将属性传递给孩子
            onBindView(text, iconResId, selectorResId, textColor, textSize);
            typedArray.recycle();
        }

        // 当前单选按钮放入到单选按钮组中
        selectorGroup=SelectorGroup.getInstance();
        selectorGroup.addSelector(this);

    }

    protected abstract View onCreateView();

    //父类读取自定义属性后通过该函数传递给子类
    protected abstract void onBindView(String text, int iconResId, int indicatorResId, int textColorResId, int textSize);

    @Override
    public void onClick(View v) {
        //获取当前状态
        boolean isSelect=switchSelector();

        //单选按钮将选中状态告诉单选组
        if (selectorGroup != null) {
            selectorGroup.setSelected(this);
        }
        if (stateListener != null) {
            stateListener.onStateChange(this, isSelect);
        }
    }

    public boolean switchSelector()
    {
        boolean isSelect=this.isSelected();
        this.setSelected(!isSelect);
        onSwitchSelected(!isSelect);
        return !isSelect;
    }

    protected abstract void onSwitchSelected(boolean isSelect);


    //利用tag生成哈希码，遂每个单选按钮的tag需保证唯一
    @Override
    public int hashCode() {
        return this.tag.hashCode();
    }

    @Override
    public boolean equals(Object obj) {
        if (obj instanceof Selector) {
            return ((Selector) obj).tag.equals(this.tag);
        }
        return false;
    }

    public interface OnSelectorStateListener {
        void onStateChange(Selector selector, boolean isSelect);
    }

    public Selector setOnSelectorStateListener(OnSelectorStateListener stateListener) {
        this.stateListener = stateListener;
        return this;
    }

    /*
    * 清除控件
    * */
    public void clearSelectGroup()
    {
        selectorGroup.clear();
    }

    @Override
    public String getTag() {
        return tag == null ? "" : tag;
    }
}
